public class PercolationStats {
    public static void main(String[] args){
        PercolationStats ps = new PercolationStats(100,100);
    }

    public PercolationStats(int N, int T){
        double[] stats = new double[T];
        //Get statistics for T runs of percolation with size N.
        double siteweight = 1.0/(N*N);
        Stopwatch stopw = new Stopwatch();
        for(int i=0; i<T; i++){
            Percolation p = new Percolation(N);
            double count = 0;
            while (!p.percolates()) {
                while(true){
                    int x = StdRandom.uniform(p.getN());
                    int y = StdRandom.uniform(p.getN());
                    if(!p.isOpen(x,y)){
                        p.open(x,y);
                        count+=siteweight;
                        break;
                    }
                }
            }
            stats[i] = count;
        }
        //Calculate stat variables
        double sum = 0;
        for(double s : stats){
            sum += s;
        }
        double avg = sum/T;
        sum = 0;
        for(double s : stats){
            sum += (s-avg)*(s-avg);
        }
        double stddev = Math.sqrt(sum/(T-1));
        double confdev = 1.96*stddev/Math.sqrt(T);
        StdOut.println("mean percolation threshold = "+avg);
        StdOut.println("stddev = "+stddev);
        StdOut.println("95% confidence interval = ["+(avg-confdev)+","+(avg+confdev)+"]");
        StdOut.println("");
    }
}
